from django.db import models
from user.models import User

# Create your models here.
DIMENSION_ONCE = -1
DIMENSION_YEAR = 0
DIMENSION_MONTH = 1
DIMENSION_WEEK = 2
DIMENSION_QUARTERLY = 3
DIMENSION_DAY = 4
DIMENSION_HOUR = 5
DIMENSION_MINUTE = 6
DIMENSION_WORKDAY = 7
DIMENSION_HOLIDAY = 8
DIMENSION_COUNTDOWN = 9
DIMENSION_CUSTOM_DAY = 10
DIMENSION_CUSTOM_MINUTE = 11

DIMENSION = (
    (DIMENSION_YEAR, '年'),
    (DIMENSION_MONTH, '月'),
    (DIMENSION_WEEK, '周'),
    (DIMENSION_QUARTERLY, '季度'),
    (DIMENSION_DAY, '天'),
    (DIMENSION_HOUR, '小时'),
    (DIMENSION_MINUTE, '分钟'),
    (DIMENSION_WORKDAY, '工作日'),
    (DIMENSION_HOLIDAY, '节假日'),
    (DIMENSION_COUNTDOWN, '倒计时'),
    (DIMENSION_CUSTOM_DAY, '自定义(天)'),
    (DIMENSION_CUSTOM_MINUTE, '自定义(分)'),
    (DIMENSION_ONCE, '单次'),
)

ENABLED = 1
DISABLE = 0
ACTIVE = (
    (DISABLE, '关闭'),
    (ENABLED, '启用'),
)


# Create your models here.
class Classify(models.Model):
    """
    分类
    """
    name = models.CharField(max_length=20, verbose_name='分类名称')
    parent = models.ForeignKey('self', null=True, on_delete=models.SET_NULL, db_constraint=False,
                               related_name='classify', verbose_name='父级分类')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '分类'
        verbose_name_plural = verbose_name
        db_table = 'reminder_classify'


class NotifyWay(models.Model):
    """
    通知方式
    """
    name = models.CharField(max_length=50, verbose_name='通知方式')
    config = models.JSONField(default=dict, verbose_name='配置')
    active = models.SmallIntegerField(choices=ACTIVE, default=ENABLED, verbose_name='开启状态')
    remark = models.CharField(max_length=200, null=True, blank=True, verbose_name='说明')

    class Meta:
        verbose_name = '通知方式'
        verbose_name_plural = verbose_name
        db_table = 'reminder_notify_way'


class Prompt(models.Model):
    """
    提示
    """
    name = models.CharField(max_length=100, verbose_name='提示名称')
    classify = models.ForeignKey(Classify, null=True, on_delete=models.SET_NULL, db_constraint=False,
                                 related_name='prompt', verbose_name='分类')

    class Meta:
        verbose_name = '提示'
        verbose_name_plural = verbose_name
        db_table = 'reminder_prompt'


class Template(models.Model):
    """
    发送消息模板（根据通知方式和提示类型）
    """
    name = models.CharField(max_length=50, default='', verbose_name='模板名称')
    prompt = models.ForeignKey(Prompt, null=True, on_delete=models.SET_NULL, db_constraint=False,
                               related_name='prompt', verbose_name='提示')
    notify_way = models.ForeignKey(NotifyWay, on_delete=models.SET_NULL, null=True, db_constraint=False,
                                   related_name='template',
                                   verbose_name='通知方式')
    content = models.JSONField(default=dict, verbose_name='模板内容')

    class Meta:
        verbose_name = '模板'
        verbose_name_plural = verbose_name
        db_table = 'reminder_template'


class Task(models.Model):
    """
    提醒任务（个人）
    """
    name = models.CharField(max_length=50, verbose_name='提醒名称')
    user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, db_constraint=False, related_name='task',
                             verbose_name='用户')
    template = models.ForeignKey(Template, null=True, on_delete=models.SET_NULL, db_constraint=False,
                                 related_name='task', verbose_name='模板')
    prompt = models.ForeignKey(Prompt, null=True, on_delete=models.SET_NULL, db_constraint=False,
                               related_name='task', verbose_name='提示')
    classify = models.ManyToManyField(Classify, db_constraint=False, related_name='task', verbose_name='分类')
    notify_way = models.ManyToManyField(NotifyWay, db_constraint=False, related_name='task', verbose_name='通知方式')
    dimension = models.SmallIntegerField(choices=DIMENSION, default=DIMENSION_DAY, verbose_name='维度')
    period = models.IntegerField(default=1, verbose_name='自定义周期值')
    value = models.CharField(max_length=50, null=True, blank=True, verbose_name='值')
    condition = models.JSONField(default=dict, null=True, blank=True, verbose_name='其它配置')
    in_advance = models.IntegerField(default=0, verbose_name='提前提醒周期')
    active = models.SmallIntegerField(choices=ACTIVE, default=ENABLED, verbose_name='开启状态')
    remark = models.CharField(max_length=200, null=True, blank=True, verbose_name='说明')
    affirm = models.BooleanField(default=False, verbose_name='是否需要确认')
    recent_exec_dt = models.DateTimeField(verbose_name='待执行时间')
    is_expire = models.BooleanField(default=False, verbose_name='是否过期')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '提醒任务'
        verbose_name_plural = verbose_name
        db_table = 'reminder_task'
